﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ferreteria.Clases;

namespace Ferreteria.Forms.GestionArticulos
{
    public partial class FrmEditarBonificacion : Form
    {
        public FrmEditarBonificacion(DataGridViewSelectedRowCollection Articulos)
        {
            InitializeComponent();

            foreach (DataGridViewRow art in Articulos)
            {
                int i = dgvArt.Rows.Add();

                dgvArt.Rows[i].Cells["art_id"].Value = art.Cells["art_id"].Value;
                dgvArt.Rows[i].Cells["art_nombre"].Value = art.Cells["art_nombre"].Value;
                dgvArt.Rows[i].Cells["art_bonif_ant"].Value = art.Cells["art_bonificacion"].Value;
                dgvArt.Rows[i].Cells["art_bonif_nueva"].Value = art.Cells["art_bonificacion"].Value;
            }
        }

        private void numVariacion_ValueChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow art in dgvArt.Rows)
            {
                decimal bonif_ant = (decimal)art.Cells["art_bonif_ant"].Value * 100;
                art.Cells["art_bonif_nueva"].Value = Funcion.CalcularBonificacion(bonif_ant, numVariacion.Value) / 100;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                using (var context = new DisancoEntities(Ferreteria.Clases.Constantes.cadenaConexion))
                {
                    bool check = true;
                    int art_id = 0;

                    foreach (DataGridViewRow fila in dgvArt.Rows)
                    {
                        art_id = (int)fila.Cells["art_id"].Value;
                        decimal nueva_bonif = (decimal)fila.Cells["art_bonif_nueva"].Value;

                        Articulos art = context.Articulos.FirstOrDefault(a => a.art_id == art_id);

                        if (art != null)
                        {
                            art.art_bonificacion = nueva_bonif;
                        }
                        else
                        {
                            check = false;
                            break;
                        }
                    }

                    if (check)
                    {
                        context.SaveChanges();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Ha ocurrido un error al tratar de obtener el registro del articulo de id " + art_id.ToString() + ". No se guardaron los cambios");
                    }

                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
    }
}
